package com.vay.de.dang.online;

import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.URLSpan;

import com.vay.de.dang.online.databinding.ActivityPerBinding;
import com.vay.de.dang.online.ui.BaseActivity;
import com.vay.de.dang.online.ui.HomeActivity;
import com.vay.de.dang.online.util.ClickURLSpan;
import com.vay.de.dang.online.util.PermissionUtil;
import com.vay.de.dang.online.util.VnDefine;
import com.vay.de.dang.online.util.VnUIManager;
import com.vay.de.dang.online.util.WebViewUtil;
import com.vay.de.dang.online.web.BaseApi;

public class ReqPerActivity extends BaseActivity {

    private ActivityPerBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityPerBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        WebViewUtil.setting(binding.webView);
        binding.webView.loadUrl(VnDefine.PermissionUrl);

        String html = getString(R.string.login_agree)
                + "<a href=\"" + VnDefine.PrivacyPolicyUrl + "\"><font color=\"#FF2929\">" + getString(R.string.label_privacy_agreement) + "</font></a>";
        binding.privacy.setText(Html.fromHtml(html));
        binding.privacy.setMovementMethod(LinkMovementMethod.getInstance());
        Spannable cbText = (Spannable) binding.privacy.getText();
        int end = cbText.length();
        URLSpan[] urls = cbText.getSpans(0, end, URLSpan.class);
        SpannableStringBuilder style = new SpannableStringBuilder(cbText);
        style.clearSpans();
        for (URLSpan url : urls) {
            ClickURLSpan urlSpan = new ClickURLSpan(mActivity, url.getURL());
            style.setSpan(urlSpan, cbText.getSpanStart(url), cbText.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        binding.privacy.setText(style);

        findViewById(R.id.bnt_agree).setOnClickListener(view -> {

            // 埋点
            BaseApi.execBuriedPoint(11030, 11000, "授权页面", "点击同意隐私协议");

            if (!PermissionUtil.checkPermission(mActivity, PermissionUtil.getApplyPermission())) {
                PermissionUtil.requestAll(mActivity, (allGranted, grantedList, deniedList) -> {
                    if (grantedList.size() > 0) {
                        mPf.putBoolean(VnDefine.ReadKeyPermission, true);
                    }
                    VnUIManager.nextActivity(mActivity, HomeActivity.class, true);
                });
            } else {
                VnUIManager.nextActivity(mActivity, HomeActivity.class, true);
            }
        });

        findViewById(R.id.bnt_cancel).setOnClickListener(view -> {

            // 埋点
            BaseApi.execBuriedPoint(11031, 11000, "授权页面", "客户拒绝授权");

            VnUIManager.nextActivity(mActivity, HomeActivity.class, true);
        });
    }
}
